package hutech.ksv.dao;

import hutech.ksv.entity.ChuNhaTroEntity;
import hutech.ksv.entity.ChuQuanAnEntity;
import hutech.ksv.entity.CongTyTuyenDungEntity;
import hutech.ksv.entity.SinhVienEntity;
import hutech.ksv.entity.TaiKhoanCNTENtity;
import hutech.ksv.entity.TaiKhoanCQAEntity;
import hutech.ksv.entity.TaiKhoanCTEntity;
import hutech.ksv.entity.TaiKhoanSVEntity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserAccountDao implements Serializable {

	@Autowired
	DataSource dataSource;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public List<TaiKhoanSVEntity> getTaiKhoanSV() {
		String query = "SELECT * FROM `taikhoansv`";
		List<TaiKhoanSVEntity> listTaiKhoanSV = new ArrayList<TaiKhoanSVEntity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				TaiKhoanSVEntity tk = new TaiKhoanSVEntity();
				tk.setTenTKSV(rs.getString("TenTKSV"));
				tk.setMatKhauTKSV(rs.getString("MatKhauSV"));
				tk.setMaSV(rs.getInt("MaSV"));
				listTaiKhoanSV.add(tk);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listTaiKhoanSV;
	}

	public List<TaiKhoanCNTENtity> getTaiKhoanCNT() {
		String query = "SELECT * FROM `taikhoancnt`";
		List<TaiKhoanCNTENtity> listTaiKhoanSV = new ArrayList<TaiKhoanCNTENtity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				TaiKhoanCNTENtity tk = new TaiKhoanCNTENtity();
				tk.setTenTKCNT(rs.getString("TenTKCNT"));
				tk.setMatKhauCNT(rs.getString("MatKhauCNT"));
				tk.setMaCNT(rs.getInt("MaCNT"));
				listTaiKhoanSV.add(tk);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listTaiKhoanSV;
	}

	public List<TaiKhoanCQAEntity> getTaiKhoanCQA() {
		String query = "SELECT * FROM `taikhoancqa`";
		List<TaiKhoanCQAEntity> listTaiKhoanSV = new ArrayList<TaiKhoanCQAEntity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				TaiKhoanCQAEntity tk = new TaiKhoanCQAEntity();
				tk.setTenTKCQA(rs.getString("TenTKCQA"));
				tk.setMatKhauCQA(rs.getString("MatKhauCQA"));
				tk.setMaCQA(rs.getInt("MaCQA"));
				listTaiKhoanSV.add(tk);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listTaiKhoanSV;
	}

	public List<TaiKhoanCTEntity> getTaiKhoanCT() {
		String query = "SELECT * FROM `taikhoanct`";
		List<TaiKhoanCTEntity> listTaiKhoanSV = new ArrayList<TaiKhoanCTEntity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				TaiKhoanCTEntity tk = new TaiKhoanCTEntity();
				tk.setTenTKCT(rs.getString("TenTKCT"));
				tk.setMatKhauCT(rs.getString("MatKhauCT"));
				tk.setMaCTTD(rs.getInt("MaCTTD"));
				listTaiKhoanSV.add(tk);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listTaiKhoanSV;
	}
	
	//them user
	public int themSinhVien(SinhVienEntity sinhVienEntity) {
		String query = "INSERT INTO `SinhVien` (`TenSV`,`DiaChiSV`,`NgaySinhSV`,`TruongSV`,`DiaChiTruongSV`,"
				+ "`MSSV`,`SDTSV`,`GioiTinhSV`,`EmailSV`) VALUES (?,?,?,?,?,?,?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, sinhVienEntity.getTenSV());
			preparedStatement.setString(2, sinhVienEntity.getDiaChiSV());
			preparedStatement.setDate(3, sinhVienEntity.getNgaySinhSV());
			preparedStatement.setString(4, sinhVienEntity.getTruongSV());
			preparedStatement.setString(5, sinhVienEntity.getDiaChiTruongSV());
			preparedStatement.setString(6, sinhVienEntity.getMssv());
			preparedStatement.setString(7, sinhVienEntity.getSdtSV());
			preparedStatement.setString(8, sinhVienEntity.getGioiTinhSV());
			preparedStatement.setString(9, sinhVienEntity.getEmailSV());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	public int themChuNhaTro(ChuNhaTroEntity chuNhaTroEntity) {
		String query = "INSERT INTO `ChuNhaTro` (`TenCNT`,`DiaChiCNT`,`GioiTinhCNT`,"
				+ "`SDTCNT`,`EmailCNT`) VALUES (?,?,?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, chuNhaTroEntity.getTenCNT());
			preparedStatement.setString(2, chuNhaTroEntity.getDiaChiCNT());
			preparedStatement.setString(3, chuNhaTroEntity.getGioiTinhCNT());
			preparedStatement.setString(4, chuNhaTroEntity.getSdtCNT());
			preparedStatement.setString(5, chuNhaTroEntity.getEmailCNT());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public int themCongTyTuyenDung(CongTyTuyenDungEntity congTyEntity) {
		String query = "INSERT INTO `congtytuyendung` (`TenCT`,`DiaChiCT`,"
				+ "`SDTCT`,`EmailCT`) VALUES (?,?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, congTyEntity.getTenCT());
			preparedStatement.setString(2, congTyEntity.getDiaChiCT());
			preparedStatement.setString(3, congTyEntity.getSdtCT());
			preparedStatement.setString(4, congTyEntity.getEmailCT());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public int themChuQuanAn(ChuQuanAnEntity chuQuanAnEntity) {
		String query = "INSERT INTO `ChuQuanAn` (`TenCQA`,`DiaChiCQA`,`GioiTinhCQA`,"
				+ "`SDTCQA`,`EmailCQA`) VALUES (?,?,?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString(1, chuQuanAnEntity.getTenCQA());
			preparedStatement.setString(2, chuQuanAnEntity.getDiaChiCQA());
			preparedStatement.setString(3, chuQuanAnEntity.getGioiTinhCQA());
			preparedStatement.setString(4, chuQuanAnEntity.getSdtCQA());
			preparedStatement.setString(5, chuQuanAnEntity.getEmailCQA());
			preparedStatement.execute();
			ResultSet rs = preparedStatement.getGeneratedKeys();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	//ket thuc them user
	
	//them tai khoan
	public int themTaiKhoanSV(TaiKhoanSVEntity taikhoanSV) {
		String query = "INSERT INTO `taikhoansv` (`TenTKSV`,`MatKhauSV`,`MaSV`) "
				+ "VALUES (?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, taikhoanSV.getTenTKSV());
			preparedStatement.setString(2, taikhoanSV.getMatKhauTKSV());
			preparedStatement.setInt(3, taikhoanSV.getMaSV());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int themTaiKhoanCNT(TaiKhoanCNTENtity taiKhoanCNT) {
		String query = "INSERT INTO `taikhoancnt` (`TenTKCNT`,`MatKhauCNT`,`MaCNT`) "
				+ "VALUES (?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, taiKhoanCNT.getTenTKCNT());
			preparedStatement.setString(2, taiKhoanCNT.getMatKhauCNT());
			preparedStatement.setInt(3, taiKhoanCNT.getMaCNT());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int themTaiKhoanCQA(TaiKhoanCQAEntity taiKhoanCQA) {
		String query = "INSERT INTO `taikhoancqa` (`TenTKCQA`,`MatKhauCQA`,`MaCQA`) "
				+ "VALUES (?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, taiKhoanCQA.getTenTKCQA());
			preparedStatement.setString(2, taiKhoanCQA.getMatKhauCQA());
			preparedStatement.setInt(3, taiKhoanCQA.getMaCQA());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int themTaiKhoanCongTy(TaiKhoanCTEntity taiKhoanCT) {
		String query = "INSERT INTO `taikhoanct` (`TenTKCT`,`MatKhauCT`,`MaCTTD`) "
				+ "VALUES (?,?,?)";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setString(1, taiKhoanCT.getTenTKCT());
			preparedStatement.setString(2, taiKhoanCT.getMatKhauCT());
			preparedStatement.setInt(3, taiKhoanCT.getMaCTTD());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	//ket thuc them tai khoan
	
	//Xoa thong tin vua nhap cua user khi tai khoan bi trung
	public int xoaThongTinSVVuaNhap(int maSV) {
		String query = "DELETE FROM `sinhvien` WHERE `MaSV` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, maSV);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int xoaThongTinCNTVuaNhap(int maCNT) {
		String query = "DELETE FROM `chunhatro` WHERE `MaCNT` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, maCNT);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int xoaThongTinCQAVuaNhap(int maCQA) {
		String query = "DELETE FROM `chuquanan` WHERE `MaCQA` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, maCQA);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	
	public int xoaThongTinCTVuaNhap(int maCT) {
		String query = "DELETE FROM `congtytuyendung` WHERE `MaCTTD` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, maCT);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				System.out.println(e1.getMessage());
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return 0;
	}
	//
}
